package src.Packets;

import src.Main;
import src.Players.*;
import src.World.*;

public class Shop
{
    public boolean buyItem(Player p, int itemId, int fromSlot, int amount)
    {
        if(p == null || amount <= 0 || Main.m.ge.shops.shopLists[p.myShopId] == null)
        {
            return false;
        }
        if(p.interfaceId != 3824 || itemId != (Main.m.ge.shops.shopLists[p.myShopId].getShopItems()[fromSlot] - 1))
        {
            return false;
        }
        ShopList sl = Main.m.ge.shops.shopLists[p.myShopId];
        PlayerItems pi = new PlayerItems();
        if(amount > sl.getShopItemsN()[fromSlot])
        {
            amount = sl.getShopItemsN()[fromSlot];
        }
        int slot = pi.getItemSlot(p, 995);
        if(slot == -1)
        {
            p.frame.sendMessage(p, "You don't have enough coins.");
            return false;
        }
        int shopValue = getItemShopValue(itemId);
        for(int i = amount; i > 0; i--)
        {
            if(p.itemsN[slot] < shopValue)
            {
                p.frame.sendMessage(p, "You don't have enough coins.");
                break;
            }
            if(pi.findInvSlot(p) > -1 || pi.invItemCount(p, itemId) > 0 && (Main.m.ge.item.stackable(itemId) || Main.m.ge.item.noted(itemId)))
            {
                pi.deleteItem(p, 995, pi.getItemSlot(p, 995), shopValue);
                pi.addItem(p, itemId, 1);
                sl.setShopItemsN(fromSlot, -1);
                sl.resetShopDelay(fromSlot);
                if((fromSlot + 1) > sl.shopItemsStandard && sl.getShopItemsN()[fromSlot] == 0)
                {
                    sl.emptyItemAmt(fromSlot);
                }
            }
            else
            {
                p.frame.sendMessage(p, "Not enough space in your inventory.");
                break;
            }
        }
        p.frame.resetItems(p, 3823);
        updateShop(p.myShopId);
        return true;
    }

    public boolean sellItem(Player p, int itemId, int fromSlot, int amount)
    {
        if(p == null || fromSlot < 0 || amount <= 0 || fromSlot > 27 || p.interfaceId != 3824 || itemId != p.items[fromSlot] - 1 || p.myShopId == 0)
        {
            return false;
        }
        PlayerItems pi = new PlayerItems();
        ShopList sl = Main.m.ge.shops.shopLists[p.myShopId];
        if(sl.shopSModifier > 1)
        {
            boolean isInShop = false;
            for(int i = 0; i <= sl.shopItemsStandard; i++)
            {
                if(itemId == (sl.getShopItems()[i] - 1))
                {
                    isInShop = true;
                    break;
                }
            }
            if(!isInShop)
            {
                p.frame.sendMessage(p, "You cannot sell " + Main.m.ge.item.getItemName(itemId) + " in this store.");
                return false;
            }
        }
        if(amount > p.itemsN[fromSlot] && (Main.m.ge.item.noted(p.items[fromSlot] - 1) || Main.m.ge.item.stackable(p.items[fromSlot] - 1)))
        {
            amount = p.itemsN[fromSlot];
        }
        else if(amount > pi.invItemCount(p, itemId) && !Main.m.ge.item.noted(p.items[fromSlot] - 1) && !Main.m.ge.item.stackable(p.items[fromSlot] - 1))
        {
            amount = pi.invItemCount(p, itemId);
        }
        int shopValue = Main.m.ge.item.getItemValue(itemId);
        for(int i = amount; i > 0; i--)
        {
            if(pi.findInvSlot(p) > -1)
            {
                if(!Main.m.ge.item.noted(p.items[fromSlot] - 1) && !Main.m.ge.item.stackable(p.items[fromSlot] - 1))
                {
                    pi.deleteItem(p, itemId, pi.getItemSlot(p, itemId), 1);
                }
                else
                {
                    pi.deleteItem(p, itemId, fromSlot, 1);
                }
                pi.addItem(p, 995, shopValue);
                addShopItem(p.myShopId, itemId, 1);
            }
            else
            {
                p.frame.sendMessage(p, "Not enough space in your inventory.");
                break;
            }
        }
        p.frame.resetItems(p, 3823);
        updateShop(p.myShopId);
        return true;
    }

    public void sellItemValue(Player p, int itemId)
    {
        if(Main.m.ge.item.isUntradable(itemId))
        {
            p.frame.sendMessage(p, "You cannot sell this item.");
        }
        else
        {
            boolean isInShop = false;
            ShopList sl = Main.m.ge.shops.shopLists[p.myShopId];
            if(sl.shopSModifier > 1)
            {
                for(int j = 0; j <= sl.shopItemsStandard; j++)
                {
                    if(itemId == (sl.getShopItems()[j] - 1))
                    {
                        isInShop = true;
                        break;
                    }
                }
            }
            else
            {
                isInShop = true;
            }
            if(!isInShop)
            {
                p.frame.sendMessage(p, "This shop will not buy that item.");
            }
            else
            {
                int shopValue = Main.m.ge.item.getItemValue(itemId);
                String shopAdd = "";
                if(shopValue >= 1000 && shopValue < 1000000)
                {
                    shopAdd = " (" + (shopValue / 1000) + "K)";
                }
                else if(shopValue >= 1000000)
                {
                    shopAdd = " (" + (shopValue / 1000000) + " million)";
                }
                p.frame.sendMessage(p, Main.m.ge.item.getItemName(itemId) + ": shop will buy for " + shopValue + " coins" + shopAdd);
            }
        }
    }

    public void buyItemValue(Player p, int itemId)
    {
        int shopValue = getItemShopValue(itemId);
        String shopAdd = "";
        if(shopValue >= 1000 && shopValue < 1000000)
        {
            shopAdd = " (" + (shopValue / 1000) + "K)";
        }
        else if(shopValue >= 1000000)
        {
            shopAdd = " (" + (shopValue / 1000000) + " million)";
        }
        p.frame.sendMessage(p, Main.m.ge.item.getItemName(itemId) + ": currently costs " + shopValue + " coins" + shopAdd);
    }

    private boolean addShopItem(int shopId, int itemId, int amount)
    {
        boolean added = false;
        if(amount <= 0 || shopId <= 0)
        {
            return false;
        }
        ShopList sl = Main.m.ge.shops.shopLists[shopId];
        if(sl == null)
        {
            return false;
        }
        for(int i = 0; i < sl.getShopItems().length; i++)
        {
            if((sl.getShopItems()[i] - 1) == itemId)
            {
                sl.setShopItemsN(i, amount);
                added = true;
            }
        }
        if(!added)
        {
            for(int i = 0; i < sl.getShopItems().length; i++)
            {
                if(sl.getShopItems()[i] == 0)
                {
                    sl.createShopItem(i, itemId + 1, amount);
                    break;
                }
            }
        }
        return true;
    }

    private void updateShop(int shopId)
    {
        for(Player p : Main.m.ge.players)
        {
            if(p == null)
            {
                continue;
            }
            if(p.myShopId == shopId)
            {
                p.updateShop = true;
            }
        }
    }

    private int getItemShopValue(int itemId)
    {
        for(ItemList il : Main.m.ge.item.itemLists)
        {
            if(il == null)
            {
                continue;
            }
            if(il.getItemId() == itemId)
            {
                return (il.getShopValue());
            }
        }
        return 1;
    }
}